package com.cjp.controller;

import cn.hutool.core.bean.BeanUtil;
import com.cjp.pojo.Article;
import com.cjp.pojo.PageBean;
import com.cjp.pojo.Result;
import com.cjp.pojo.dto.ArticleAddDTO;
import com.cjp.pojo.dto.ArticlePageQueryDTO;
import com.cjp.pojo.dto.ArticleUpdateDTO;
import com.cjp.pojo.vo.ArticleVO;
import com.cjp.service.IArticleService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/article")
@RequiredArgsConstructor
public class ArticleController {

    private final IArticleService articleService;

    @PostMapping
    public Result addArticle(@RequestBody @Validated ArticleAddDTO articleAddDTO) {
        articleService.addArticle(articleAddDTO);
        return Result.success();
    }

    @PutMapping
    public Result updateArticle(@RequestBody @Validated ArticleUpdateDTO articleUpdateDTO) {
        articleService.updateArticle(articleUpdateDTO);
        return Result.success();
    }

    @GetMapping("/detail")
    public Result<ArticleVO> detailArticle(@RequestParam Long id) {
        Article article = articleService.getById(id);
        ArticleVO articleVO = BeanUtil.copyProperties(article, ArticleVO.class);
        return Result.success(articleVO);
    }

    @DeleteMapping
    public Result deleteArticle(@RequestParam Long id) {
        articleService.removeById(id);
        return Result.success();
    }

    @GetMapping
    public Result<PageBean<ArticleVO>> pageQueryArticle(Integer pageNum, Integer pageSize, Long categoryId, String state) {
        PageBean<ArticleVO> pageBean = articleService.pageQueryArticle(pageNum, pageSize, categoryId, state);
        return Result.success(pageBean);
    }

}
